import { Component, OnInit } from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {HomeConfigService} from '../../../../lib/service/admin/home-config.service';
import {AliyunOssOutputUrl} from '../../../../../config/api';
import {EditHomeConfig} from '../../config'
import {AdOssUploadService} from '../../../../lib/service/ad-oss-upload.service';


@Component({
  selector: 'app-config-edit',
  templateUrl: 'config-edit.component.html',
  styleUrls: ['config-edit.component.scss'],
  providers: []
})
export class ConfigEditComponent implements OnInit {

  editHomeConfig = new EditHomeConfig;
  aliyunOssOutputUrl = AliyunOssOutputUrl;

  type: string;
  message: string;
  isShow = false;
  img_file: any;
  img_name: string;
  img_url: string;
  ewm_file: any;
  ewm_name: string;
  ewm_url: string;

  constructor(private homeConfigService: HomeConfigService, private route: ActivatedRoute,private adOssUploadService: AdOssUploadService) { }

  ngOnInit() {
    let id = +this.route.snapshot.params['id'];
    this.getDetail(id);
  }

  //上传图片
  uploadImg($event: any, imgName: string) {
    const file = $event.path[0].files[0];
    const myReader = new FileReader();
    const self = this;
    const suffix = file.name.replace(/.+\./, '').toLowerCase();
    const now = new Date();
    if (imgName == 'logo') {
      this.img_file = file;
      this.img_name = now.getTime() + '.' + suffix;
      myReader.readAsDataURL(file);
      myReader.onloadend = function (e) {
        self.img_url = myReader.result;
      };
    } else {
      this.ewm_file = file;
      this.ewm_name = now.getTime() + '.' + suffix;
      myReader.readAsDataURL(file);
      myReader.onloadend = function (e) {
        self.ewm_url = myReader.result;
      };
    }
  }

  //获取详情信息
  getDetail(id: number){
    this.homeConfigService.getHomeConfigDetail(id).subscribe(
      res => {
        if (res.code === 2000) {
          this.editHomeConfig = res.data;
          this.img_url = AliyunOssOutputUrl + '/' + res.data.site_logo;
          this.ewm_url = AliyunOssOutputUrl + '/' + res.data.weichat_ewm;
        } else {
          alert(res.code + ',' + res.message);
        }
      }
    );
  }

  submitForm(){
    this.homeConfigService.editHomeConfig(this.editHomeConfig).subscribe(
      res => {
        if (res.code === 2000) {
          if (!!this.ewm_file) {
            this.adOssUploadService.getSignature('/v1/common/setting_ewm/' + res.data.id + '?callback=true').subscribe(
              res1 => {
                this.adOssUploadService.fileUpload(this.ewm_file, res1.data, this.ewm_name, 'weichat_ewm').subscribe(
                  res2 => {
                    if (res2.status === 200) {
                      if (!!this.img_file) {
                        this.uploadLogo(res.data.id);
                      } else {
                        this.type = 'success';
                        this.message = '修改成功';
                        this.isShow = true;
                      }
                    } else {
                      this.type = 'error';
                      this.message = '图片上传失败';
                      this.isShow = true;
                    }
                  }
                );
              }
            );
          } else {
            if (!!this.img_file) {
              this.uploadLogo(res.data.id);
            } else {
              this.type = 'success';
              this.message = '修改成功';
              this.isShow = true;
            }
          }
        } else {
          this.type = 'error';
          this.message = res.message;
          this.isShow = true;
        }
      }
    )

  }

  // 上传logo
  uploadLogo(id: number) {
    this.adOssUploadService.getSignature('/v1/common/setting_logo/' + id + '?callback=true').subscribe(
      res1 => {
        this.adOssUploadService.fileUpload(this.img_file, res1.data, this.img_name, 'site_logo').subscribe(
          res2 => {
            if (res2.status === 200) {
              this.type = 'success';
              this.message = '修改成功';
              this.isShow = true;
            } else {
              this.type = 'error';
              this.message = '图片上传失败';
              this.isShow = true;
            }
          }
        );
      }
    );
  }
}
